package cn.tsinghua.comsmacitccntsinghua.estar;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.tencent.tinker.lib.tinker.TinkerInstaller;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.InputStream;

import cn.tsinghua.comsmacitccntsinghua.entity.Res;
import cn.tsinghua.comsmacitccntsinghua.utils.FileUtil;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * @Author yjl
 * @create 2021/10/15 9:22
 */
public class HttpImpl  {
    public static final String TAG = "Retrofit ----- HttpImpl";
    public static Http service;

    public static Res getHotFix(Context context){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(Http.BASE_URL)
                .build();
        service = retrofit.create(Http.class);
        service.gethotFixFile()
                .subscribeOn(Schedulers.newThread())//新线程请求
                .observeOn(Schedulers.io()) //完成后在IO线程执行
                .doOnNext(new Action1<ResponseBody>() {
                    @Override
                    public void call(ResponseBody responseBody) {
                        Log.e("rmCom","拿到热更新文件");
                        //保存文件
                        FileUtil.writeFile(responseBody);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//在主线程中执行
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {
                        Log.d("rxCom","http完成");
                        //完成
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.d("rxCom","http错误");
                        //请求失败
                    }
                    @Override
                    public void onNext(ResponseBody responseBody){
                        //完成了一次完整的请求
                        //下载文件
                        File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/patch_signed_7zip.apk");
                        //文件存在则更新
                        if(file.exists()){
                            TinkerInstaller.onReceiveUpgradePatch(context, Environment.getExternalStorageDirectory().getAbsolutePath()+"/patch_signed_7zip.apk");
                        }
                        EventBus.getDefault().post("fileRecSuc");
                    }
                });
        return null;
    }

    public static Res getColdFixFile(Context context){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(Http.BASE_URL)
                .build();
        service = retrofit.create(Http.class);
        service.getcoldFixFile()
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())
                .doOnNext(new Action1<ResponseBody>() {
                    @Override
                    public void call(ResponseBody responseBody) {
                        Log.d(TAG,"getColdFixFile() --- call");
                        FileUtil.writeFile(responseBody);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {
                        Log.d(TAG,"getColdFixFile() --- onNext");
                        //完成
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG,"getColdFixFile() --- onError");
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        Log.d(TAG,"getColdFixFile() --- onError");
                        //写入文件
                        File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/app.apk");
                        if(file.exists()){
                            //TODO 安装并且重启

                        }
                    }
                });
        return null;
    }

    public static Res uploadIllegalAction(){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(HttpXY.BASE_URL)
                .build();
        HttpXY service = retrofit.create(HttpXY.class);
        service.uploadIllegalAction()
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())
                .doOnNext(new Action1<ResponseBody>() {
                    @Override
                    public void call(ResponseBody responseBody) {
                        Log.d(TAG,"uploadIllegalAction() --- call");
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {
                        Log.d(TAG,"uploadIllegalAction() --- onNext");
                        //完成
                    }

                    @Override
                    public void onError(Throwable e) {

                        Log.d(TAG,"uploadIllegalAction() --- onError");

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {

                        Log.d(TAG,"uploadIllegalAction() --- onError");

                    }
                });
        return null;
    }


}

